﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Services;
using SpaceBattle.Logic;
using SpaceBattle.Menu;
using Utilities.Menu.UIElements;
using SpaceBattle.Resources;
using SpaceBattle.Logic.SampleLevel;

namespace SpaceBattle
{

  using Game = Microsoft.Xna.Framework.Game;
  using Utilities;

  /// <summary>
  /// La classe UIRenderer si occupa di disegnare l'interfaccia: punteggi, informazioni utili, bottoni (il pulsante "QUIT GAME") etc.
  /// UIRenderer utilizza il servizio di IStateProvider per conoscere punteggio, numero di vite residue del giocatore, etc.
  /// </summary>
  public class UILandscape : DrawableGameComponent
  {
    public static int PrimaryActive = 0, SecondaryActive = 0;

    Matrix screen_transform;
    SpriteBatch sprite_batch;
    SpriteFont title_font, arial14, menu_f;
    Texture2D white_pixel, ship_icon, life_bar;
    Button resume, quit_to_menu, controls;
    SoundEffect click;

    float alpha = 0.0f;
    Texture2D single_tap, double_tap, long_tap;
    SpriteFont font_32, font_16, font12;
    Button back;
    Button[] weapons, wtype;
    Box box;
    GameShipConfig ships;
    Texture2D ui_dock, overlay_dock, bar_block, score_dock;
    Rectangle pause_rect;
    Vector2 box_dimension = new Vector2(200, 170);
    Vector2 initial_box_pos = new Vector2(1100, 252.5f), final_box_pos = new Vector2(590, 252.5f);
    GraphicsDeviceManager graphics;

    GameMode mode;
    PlayState state;

    public UILandscape(Microsoft.Xna.Framework.Game game, GameMode mode)
      : base(game)
    {
      this.UpdateOrder = (int)ComponentOrders.UI;
      this.DrawOrder = (int)ComponentOrders.UI;

      this.mode = mode;
    }

    IStateProvider<BaseLevelStats> state_provider;
    CameraProvider camera;
    InputManager input_manager;
    public static ShipType last_ship_selected = ShipType.PlayerBaseShip;
    public override void Initialize()
    {
      state_provider = Game.Services.GetService(typeof(IStateProvider<BaseLevelStats>))
        as IStateProvider<BaseLevelStats>;
      (Game.Services.GetService(typeof(IInputManager)) as IInputManager).OnTap += new InputManager.Tap(on_tap);
      graphics = (GraphicsDeviceManager)Game.Services.GetService(typeof(GraphicsDeviceManager));

      input_manager = Game.Services.GetService(typeof(IInputManager)) as InputManager;

      camera = Game.Services.GetService(typeof(CameraProvider)) as CameraProvider;

      var player_ship_index = (int)state_provider.CurrentState.player.AsShip.ship_type;

      if (mode == GameMode.Campaign)
        ships = Leaderboard.GetCampaignState().CampaignState.Ships[player_ship_index];
      else
        ships = Leaderboard.GetCampaignState().Ships[player_ship_index];

      state = PlayState.Playing;

      base.Initialize();
    }

    protected override void Dispose(bool disposing)
    {
      Game.Services.RemoveService(typeof(IInputManager));

      //graphics.SupportedOrientations = DisplayOrientation.Portrait;
      //graphics.PreferredBackBufferWidth = 480;
      //graphics.PreferredBackBufferHeight = 800;

      //graphics.ApplyChanges();

      base.Dispose(disposing);
    }

    protected override void LoadContent()
    {
      sprite_batch = (SpriteBatch)Game.Services.GetService(typeof(SpriteBatch));
      screen_transform = Matrix.CreateScale(GraphicsDevice.Viewport.Width / 800,
          GraphicsDevice.Viewport.Height / 480, 1.0f);

      title_font = Game.Content.Load<SpriteFont>(@"fonts\OCRAExtended28");
      arial14 = Game.Content.Load<SpriteFont>(@"fonts\arial14ptx");
      menu_f = Game.Content.Load<SpriteFont>(@"fonts\OCRAExtended18");
      white_pixel = Game.Content.Load<Texture2D>(@"UI\whitepixel");
      ship_icon = Game.Content.Load<Texture2D>(@"UI\ship_ico_v");
      click = Game.Content.Load<SoundEffect>(@"Audio\button");
      ui_dock = Game.Content.Load<Texture2D>(@"UI\upper_dock_v");
      score_dock = Game.Content.Load<Texture2D>(@"UI\score_dock");
      menu_dock = Game.Content.Load<Texture2D>(@"UI\menu_dock");
      bar_block = Game.Content.Load<Texture2D>(@"UI\bar_block_v");
      life_bar = Game.Content.Load<Texture2D>(@"UI\life_bar");
      yellow_glow = Game.Content.Load<Texture2D>(@"UI\WSelector\yellow_glow");
      white_glow = Game.Content.Load<Texture2D>(@"UI\WSelector\white_glow");
      f_weapon_circle = Game.Content.Load<Texture2D>(@"UI\WSelector\weapon_circle_v");
      s_weapon_circle = Game.Content.Load<Texture2D>(@"UI\WSelector\b_weapon_circle");
      overlay_dock = Game.Content.Load<Texture2D>(@"UI\upper_dock_overlay");

      double_tap = Game.Content.Load<Texture2D>("Menu/double_tap");
      single_tap = Game.Content.Load<Texture2D>("Menu/single_tap");
      long_tap = Game.Content.Load<Texture2D>("Menu/long_tap");
      font_32 = Game.Content.Load<SpriteFont>("Fonts/OCRAExtended30");
      font_16 = Game.Content.Load<SpriteFont>("Fonts/OCRAExtended16");
      font12 = Game.Content.Load<SpriteFont>("Fonts/OCRAExtended12");

      f_weapon_circle_pos = new Vector2(GraphicsDevice.Viewport.Width / 2.0f - f_weapon_circle.Width / 2.0f,
          GraphicsDevice.Viewport.Height / 2.0f - f_weapon_circle.Height / 2.0f);
      s_weapon_circle_pos = new Vector2(GraphicsDevice.Viewport.Width / 2.0f - s_weapon_circle.Width / 2.0f,
          GraphicsDevice.Viewport.Height / 2.0f - s_weapon_circle.Height / 2.0f);
      half_yglow_vec = new Vector2(yellow_glow.Width / 2.0f, yellow_glow.Height / 2.0f);
      score_dock_pos = new Vector2(GraphicsDevice.Viewport.Width - score_dock.Width, 0);
      menu_pos = new Vector2(GraphicsDevice.Viewport.Width - menu_dock.Width, GraphicsDevice.Viewport.Height - menu_dock.Height);
      overlay_dock_pos = Vector2.Zero;

      tap_text_pos = new Vector2(GraphicsDevice.Viewport.Width / 2.0f - menu_f.MeasureString(tap_text).X / 2.0f,
          GraphicsDevice.Viewport.Height / 2.0f + s_weapon_circle.Height / 2.0f + 20.0f);

      pause_rect = new Rectangle(GraphicsDevice.Viewport.Width - menu_dock.Width,
          GraphicsDevice.Viewport.Height - menu_dock.Height, menu_dock.Width, menu_dock.Height);

      var resume_sm = menu_f.MeasureString(Strings.Resume);
      resume = new Button
      {
        CollisionBox = new Rectangle(590 + (100 - (int)resume_sm.X / 2) - 10, 261, (int)resume_sm.X + 20, (int)resume_sm.Y + 20),
        Label = Strings.Resume,
        Position = new Vector2(590 + (100 - resume_sm.X / 2.0f), 271)
      };
      var control_sm = menu_f.MeasureString(Strings.PauseMenu_Controls);
      controls = new Button
      {
        CollisionBox = new Rectangle(590 + (100 - (int)control_sm.X / 2), 311, (int)control_sm.X + 20, (int)control_sm.Y + 20),
        Label = Strings.PauseMenu_Controls,
        Position = new Vector2(590 + (100 - control_sm.X / 2.0f), 321)
      };
      var quit_sm = menu_f.MeasureString(Strings.Quit);
      quit_to_menu = new Button
      {
        CollisionBox = new Rectangle(590 + (100 - (int)quit_sm.X / 2), 361, (int)quit_sm.X + 20, (int)quit_sm.Y + 20),
        Label = Strings.Quit,
        Position = new Vector2(590 + (100 - quit_sm.X / 2.0f), 371)
      };

      var back_sm = font_32.MeasureString(Strings.Back);
      back = new Button
      {
        CollisionBox = new Rectangle((int)(GraphicsDevice.Viewport.Width / 2.0f - back_sm.X / 2.0f) - 10, 410,
            (int)back_sm.X + 20, (int)back_sm.Y + 20),
        Label = Strings.Back,
        Position = new Vector2(GraphicsDevice.Viewport.Width / 2.0f - back_sm.X / 2.0f, 420.0f)
      };

      box = new Box(new Texture2D[] { Game.Content.Load<Texture2D>("UI/pause_menu_border"),
            Game.Content.Load<Texture2D>("UI/WhitePixel"), Game.Content.Load<Texture2D>("UI/pause_menu_border") },
          initial_box_pos, box_dimension, Color.Black, 0.7f);

      weapons = new Button[11];

      weapons[0] = new Button { CollisionBox = new Rectangle(473 - 47, 141 - 43, 94, 86), Label = "", Position = new Vector2(473, 141), Pressed = false };
      weapons[1] = new Button { CollisionBox = new Rectangle(512 - 47, 282 - 43, 94, 86), Label = "", Position = new Vector2(512, 282), Pressed = false };
      weapons[2] = new Button { CollisionBox = new Rectangle(407 - 47, 359 - 43, 94, 86), Label = "", Position = new Vector2(407, 359), Pressed = false };
      weapons[3] = new Button { CollisionBox = new Rectangle(294 - 47, 275 - 43, 94, 86), Label = "", Position = new Vector2(294, 275), Pressed = false };
      weapons[4] = new Button { CollisionBox = new Rectangle(333 - 47, 142 - 43, 94, 86), Label = "", Position = new Vector2(333, 142), Pressed = false };
      weapons[5] = new Button { CollisionBox = new Rectangle(478 - 47, 137 - 43, 94, 86), Label = "", Position = new Vector2(478, 137), Pressed = false };
      weapons[6] = new Button { CollisionBox = new Rectangle(522 - 47, 261 - 43, 94, 86), Label = "", Position = new Vector2(522, 261), Pressed = false };
      weapons[7] = new Button { CollisionBox = new Rectangle(447 - 47, 349 - 43, 94, 86), Label = "", Position = new Vector2(447, 349), Pressed = false };
      weapons[8] = new Button { CollisionBox = new Rectangle(322 - 47, 326 - 43, 94, 86), Label = "", Position = new Vector2(322, 326), Pressed = false };
      weapons[9] = new Button { CollisionBox = new Rectangle(283 - 47, 224 - 43, 94, 86), Label = "", Position = new Vector2(283, 224), Pressed = false };
      weapons[10] = new Button { CollisionBox = new Rectangle(351 - 47, 134 - 43, 94, 86), Label = "", Position = new Vector2(351, 134), Pressed = false };

      ChangeActiveWeapon((int)state_provider.CurrentState.player.AsShip.ship_description.PrimaryWeapons[PrimaryActive].Type);
      ChangeActiveWeapon((int)state_provider.CurrentState.player.AsShip.ship_description.SecondaryWeapons[SecondaryActive].Type);

      if (last_ship_selected == state_provider.CurrentState.player.AsShip.ship_type)
      {
        state_provider.CurrentState.player.primary_active = PrimaryActive;
        state_provider.CurrentState.player.secondary_active = SecondaryActive;
      }

      last_ship_selected = state_provider.CurrentState.player.AsShip.ship_type;

      wtype = new Button[2];

      wtype[1] = new Button { CollisionBox = new Rectangle(437, 216, 61, 40), Label = "", Position = new Vector2(467, 236), Pressed = false };
      wtype[0] = new Button { CollisionBox = new Rectangle(312, 216, 61, 40), Label = "", Position = new Vector2(342, 236), Pressed = false };

      life_rect = new Rectangle(-72, 32, life_bar.Width, life_bar.Height);
    }

    public override void Update(GameTime gameTime)
    {
      box.Update((float)gameTime.ElapsedGameTime.TotalSeconds * 3);

      if (state == PlayState.Pause || state == PlayState.WeaponSelection) state_provider.Enabled = false;

      base.Update(gameTime);
    }

    void on_tap(Vector2 tap_position)
    {
      var t_p = tap_position.ToPoint(screen_transform);
      var play_click = false;

      switch (state)
      {
        case PlayState.Controls:
          if (back.CollisionBox.Contains(t_p) || input_manager.IsBackButtonPressed())
          {
            state = PlayState.Pause;
            play_click = true;
          }
          break;
        case PlayState.Pause:
          state_provider.Enabled = false;
          if (resume.CollisionBox.Contains(t_p) || input_manager.IsBackButtonPressed())
          {
            state_provider.Enabled = true;
            state = PlayState.Playing;
            box.StartTransition(initial_box_pos, box_dimension);
            play_click = true;
          }
          else if (controls.CollisionBox.Contains(t_p))
          {
            play_click = true;
            state = PlayState.Controls;
          }
          else if (quit_to_menu.CollisionBox.Contains(t_p))
          {
            play_click = true;
            if(OnQuitToMenu != null)
              OnQuitToMenu();
          }
          break;
        case PlayState.WeaponSelection:
          for (int i = 0; i < wtype.Length; i++)
          {
            if (wtype[i].CollisionBox.Contains(t_p))
            {
              wtype[i].Pressed = true;
              wtype[Math.Abs(i - 1)].Pressed = false;
              click.Play(AudioManager.Volume, 0.0f, 0.0f);
              return;
            }
          }
          if (wtype[0].Pressed)
          {
            for (int i = 0; i < state_provider.CurrentState.player.AsShip.ship_description.PrimaryWeapons.Count; i++)
              if (weapons[(int)state_provider.CurrentState.player.AsShip.ship_description.PrimaryWeapons[i].Type].CollisionBox.Contains(t_p) &&
                state_provider.CurrentState.player.AsShip.ship_description.PrimaryWeapons[i].MinLevelRequired <=
                state_provider.CurrentState.player.AsShip.ship_description.Level)
              {
                click.Play(AudioManager.Volume, 0.0f, 0.0f);
                ChangeActiveWeapon((int)state_provider.CurrentState.player.AsShip.ship_description.PrimaryWeapons[i].Type);
                state_provider.CurrentState.player.primary_active = i;
                CloseWeaponSelector();
                return;
              }
          }
          if (wtype[1].Pressed)
            for (int i = 0; i < state_provider.CurrentState.player.AsShip.ship_description.SecondaryWeapons.Count; i++)
              if (weapons[(int)state_provider.CurrentState.player.AsShip.ship_description.SecondaryWeapons[i].Type].CollisionBox.Contains(t_p) &&
                state_provider.CurrentState.player.AsShip.ship_description.PrimaryWeapons[i].MinLevelRequired <=
                state_provider.CurrentState.player.AsShip.ship_description.Level)
              {
                click.Play(AudioManager.Volume, 0.0f, 0.0f);
                ChangeActiveWeapon((int)state_provider.CurrentState.player.AsShip.ship_description.SecondaryWeapons[i].Type);
                state_provider.CurrentState.player.secondary_active = i;
                CloseWeaponSelector();
                return;
              }
          if (input_manager.IsBackButtonPressed() || Vector2.Distance(tap_position, new Vector2(240, 400)) > s_weapon_circle.Width / 2.0f + 20)
            CloseWeaponSelector();
          break;
        case PlayState.Playing:
          if (pause_rect.Contains(t_p) || input_manager.IsBackButtonPressed())
          {
            state_provider.Enabled = false;
            state = PlayState.Pause;
            box.StartTransition(final_box_pos, box_dimension);
            play_click = true;
            state_provider.CurrentState.player.waypoint.HasValue = false;
          }
          else if (Vector2.Distance(tap_position, PlayerShipPos()) < 35)
          {
            state = PlayState.WeaponSelection;
            state_provider.Enabled = false;
            play_click = true;
          }
          break;
      }

      if (play_click) click.Play(AudioManager.Volume, 0.0f, 0.0f);
    }

    private void CloseWeaponSelector()
    {
      state = PlayState.Playing;
      for (int i = 0; i < wtype.Length; i++) wtype[i].Pressed = false;
      click.Play(AudioManager.Volume, 0.0f, 0.0f);

      state_provider.Enabled = true;
    }

    public event Action OnQuitToMenu;

    private void ChangeActiveWeapon(int index)
    {
      int init_v = 0;
      int fin_v = index < 5 ? 5 : weapons.Length;

      if (index < 5)
      {
        fin_v = 4;
        for (int i = 0; i < state_provider.CurrentState.player.AsShip.ship_description.PrimaryWeapons.Count; i++)
          if ((int)state_provider.CurrentState.player.AsShip.ship_description.PrimaryWeapons[i].Type == index)
          {
            PrimaryActive = i;
            break;
          }
      }
      else
      {
        fin_v = weapons.Length;
        init_v = 5;
        for (int i = 0; i < state_provider.CurrentState.player.AsShip.ship_description.SecondaryWeapons.Count; i++)
          if ((int)state_provider.CurrentState.player.AsShip.ship_description.SecondaryWeapons[i].Type == index)
          {
            SecondaryActive = i;
            break;
          }
      }

      for (int i = init_v; i < fin_v; i++) weapons[i].Pressed = false;

      weapons[index].Pressed = true;
    }

    static StringBuilder score_stringB = new StringBuilder("0");

    Color final_bar_color = new Color(18, 255, 0), life_bar_color = new Color(135, 0, 3);
    public override void Draw(GameTime gameTime)
    {
      sprite_batch.Begin(SpriteSortMode.Deferred, null, null, null, null, null, screen_transform);

      switch (state)
      {
        case PlayState.Playing:
          DrawGame();
          break;
        case PlayState.WeaponSelection:
          DrawGame();
          DrawWeaponSelector();
          break;
        case PlayState.Pause:
          DrawGame();
          if (!box.InTransition) alpha = MathHelper.Clamp(alpha + (float)gameTime.ElapsedGameTime.TotalSeconds * 8.0f, 0.0f, 1.0f);
          DrawPauseMenu();
          break;
        case PlayState.Controls:
          DrawControls();
          break;
      }

      sprite_batch.End();

      #region Commented Code

      //fps_val = (int)(1.0 / gameTime.ElapsedGameTime.TotalSeconds);
      //sprite_batch.DrawNumericString(font_16, fps_string, fps_val, new Vector2(20, 70), Color.White, false);

      //CONTATORE DEL GC
      //timefromLastGC += (float)gameTime.ElapsedGameTime.TotalSeconds;
      //if (prevGCSize > GC.GetTotalMemory(false))
      //{            
      //    timefromLastGcPerm = (float)timefromLastGC;
      //    timefromLastGC = 0;
      //    lastGC = (float)gameTime.TotalGameTime.TotalSeconds;
      //    //s1.Remove(0, s1.Length);
      //    //s1.Append(timefromLastGC);
      //}
      //prevGCSize = GC.GetTotalMemory(false);
      //sprite_batch.DrawNumericString(arial14, null, timefromLastGcPerm, new Vector2(10, 10), Color.White, false);
      //sprite_batch.DrawNumericString(arial14, null, timefromLastGC, new Vector2(80, 10), Color.White, false);


      //if (lastmis > 1000)
      //{
      //    //s2.Remove(0, s2.Length);
      //    //s2.Append((((float)GC.GetTotalMemory(false) - lastGCsize) / lastmis));
      //    tassocrescita = (((float)GC.GetTotalMemory(false) - lastGCsize) / lastmis) * 10.0f;
      //    lastGCsize = (float)GC.GetTotalMemory(false);
      //    lastmis = 0;
      //}

      //lastmis += (float)gameTime.ElapsedGameTime.TotalMilliseconds;
      //sprite_batch.DrawNumericString(arial14, null, tassocrescita, new Vector2(10, 30), Color.White, false);
      //sprite_batch.DrawNumericString(arial14,null, state_provider.CurrentState.player.geometry.CollisionSkin.NonCollidables.Count, 
      //  new Vector2(10, 50), Color.White, false);

      #endregion

      base.Draw(gameTime);
    }

    #region Commented Code

    //float lastmis;
    //float lastGCsize;
    //float lastGC;
    //float timefromLastGC;
    //float timefromLastGcPerm;
    //float tassocrescita;
    //long prevGCSize;
    //float gcincrate;

    #endregion

    StringBuilder s1 = new StringBuilder(), s2 = new StringBuilder();

    #region Draw Helper

    Vector2 score_dock_pos, score_pos = new Vector2(0, 5), menu_pos, ui_dock_pos = new Vector2(0, 0);
    Rectangle life_rect;
    private void DrawGame()
    {
      life_rect.Width = (int)(life_bar.Width * (state_provider.CurrentState.player.AsShip.stats.CurrentLife / state_provider.CurrentState.player.AsShip.stats.MaxLife));

      sprite_batch.Draw(ui_dock, ui_dock_pos, Color.White);
      sprite_batch.Draw(life_bar, life_rect, life_bar_color);
      sprite_batch.Draw(overlay_dock, overlay_dock_pos, Color.White);

      sprite_batch.Draw(score_dock, score_dock_pos, Color.White);
      sprite_batch.Draw(menu_dock, menu_pos, Color.White);

      var score_val = state_provider.CurrentScore.LevelScore.ToString();
      score_pos.X = (GraphicsDevice.Viewport.Width - score_dock.Width / 2.0f) - menu_f.MeasureString(score_val).X / 2.0f;
      sprite_batch.DrawString(menu_f, score_val, score_pos, Color.White);

      var wbn = MathHelper.Clamp(state_provider.CurrentState.player.warp_state * 10, 0, 10);
      for (int i = 1; i <= wbn; i++)
        sprite_batch.Draw(bar_block, new Vector2(172 + i * (bar_block.Width), 8), Color.White);

      var ratio_to_shield = (state_provider.CurrentState.player.shield /
          (state_provider.CurrentState.player.AsShip.ship_description.MaxShield * state_provider.CurrentState.player.AsShip.ship_description.DamageMultiplier))
          * 10.0f;

      for (int i = 0; i < ratio_to_shield; i++)
        sprite_batch.Draw(bar_block, new Vector2(315 + i * (bar_block.Width), 8), new Color(1, 70, 128));

      var en = MathHelper.Clamp(state_provider.CurrentState.player.energy * 10, 0, 10);
      for (int i = 0; i < en; i++)
        sprite_batch.Draw(bar_block, new Vector2(28 + i * (bar_block.Width), 8), final_bar_color);

      sprite_batch.Draw(ship_icon, new Vector2(558, 7), Color.White);
      sprite_batch.DrawString(font_16, "x " + state_provider.CurrentState.player.num_lives, new Vector2(590, 8),
          Color.White);

      box.Draw(sprite_batch);
    }

    Texture2D f_weapon_circle, s_weapon_circle, yellow_glow, white_glow;
    string tap_text = "Tap to Close";
    Vector2 tap_text_pos, f_weapon_circle_pos, s_weapon_circle_pos, half_yglow_vec;
    private Texture2D menu_dock;
    private Vector2 overlay_dock_pos;
    private void DrawWeaponSelector()
    {
      sprite_batch.Draw(f_weapon_circle, f_weapon_circle_pos, Color.White);
      sprite_batch.DrawString(menu_f, tap_text, tap_text_pos, Color.White);
      if (wtype[0].Pressed || wtype[1].Pressed)
      {
        sprite_batch.Draw(s_weapon_circle, s_weapon_circle_pos, Color.White);

        if (wtype[0].Pressed)
        {
          sprite_batch.Draw(yellow_glow, wtype[0].Position - half_yglow_vec, Color.White);

          for (int i = 0; i < state_provider.CurrentState.player.AsShip.ship_description.PrimaryWeapons.Count; i++)
          {
            var index = (int)state_provider.CurrentState.player.AsShip.ship_description.PrimaryWeapons[i].Type;
            var t = Game.Content.Load<Texture2D>("UI/WSelector/" + ships.Weapons[index].Type.ToString());
            if (weapons[index].Pressed)
              sprite_batch.Draw(white_glow, weapons[index].Position - half_yglow_vec, Color.White);
            sprite_batch.Draw(t, weapons[index].Position - new Vector2(t.Width / 2.0f, t.Height / 2.0f), Color.White);
          }
        }
        else
        {
          sprite_batch.Draw(yellow_glow, wtype[1].Position - half_yglow_vec, Color.White);

          for (int i = 0; i < state_provider.CurrentState.player.AsShip.ship_description.SecondaryWeapons.Count; i++)
          {
            var index = (int)state_provider.CurrentState.player.AsShip.ship_description.SecondaryWeapons[i].Type;
            var t = Game.Content.Load<Texture2D>("UI/WSelector/" + ships.Weapons[index].Type.ToString());
            if (weapons[index].Pressed)
              sprite_batch.Draw(white_glow, weapons[index].Position - half_yglow_vec, Color.White);
            sprite_batch.Draw(t, weapons[index].Position - new Vector2(t.Width / 2.0f, t.Height / 2.0f), Color.White);
          }
        }
      }
    }

    private void DrawPauseMenu()
    {
      //var fm = title_font.MeasureString(Strings.GamePaused);
      //sprite_batch.Draw(pause_bg, new Vector2(CenterValue(480, pause_bg.Width), CenterValue(800, pause_bg.Height)), Color.White * alpha);
      //sprite_batch.DrawString(title_font, Strings.GamePaused, new Vector2(CenterValue(480, fm.X), 235) + Vector2.One * 2.0f, Color.SteelBlue * alpha);
      //sprite_batch.DrawString(title_font, Strings.GamePaused, new Vector2(CenterValue(480, fm.X), 235), Color.White * alpha);
      if (!box.InTransition)
      {
        sprite_batch.DrawString(menu_f, resume.Label, resume.Position, Color.White * alpha);
        sprite_batch.DrawString(menu_f, controls.Label, controls.Position, Color.White * alpha);
        sprite_batch.DrawString(menu_f, quit_to_menu.Label, quit_to_menu.Position, Color.White * alpha);
      }
    }

    private void DrawControls()
    {
      sprite_batch.Draw(white_pixel, new Rectangle(0, 0, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height),
          Color.Black * MathHelper.Min(alpha, 0.5f));

      sprite_batch.DrawString(font_16, Strings.Controls_SingleTap + "\n" + Strings.Controls_ST_Desc_OF.Replace("\\n", "\n") +
          "\n" + Strings.Controls_ST_Desc_OE.Replace("\\n", "\n") + "\n" +
          Strings.Controls_ST_Desc_OS.Replace("\\n", "\n"), new Vector2(32, 92), Color.SteelBlue * alpha); // 150
      sprite_batch.DrawString(font_16, Strings.Controls_SingleTap + "\n" + Strings.Controls_ST_Desc_OF.Replace("\\n", "\n") +
          "\n" + Strings.Controls_ST_Desc_OE.Replace("\\n", "\n") + "\n" +
          Strings.Controls_ST_Desc_OS.Replace("\\n", "\n"), new Vector2(30, 90), Color.White * alpha); // 150

      //sprite_batch.Draw(double_tap, new Vector2(85, 360), Color.White * alpha);
      sprite_batch.DrawString(font_16, Strings.Controls_DoubleTap + "\n" + Strings.Controls_DT_Desc_OE.Replace("\\n", "\n"),
          new Vector2(432, 92), Color.SteelBlue * alpha);
      sprite_batch.DrawString(font_16, Strings.Controls_DoubleTap + "\n" + Strings.Controls_DT_Desc_OE.Replace("\\n", "\n"),
          new Vector2(430, 90), Color.White * alpha);

      //sprite_batch.Draw(long_tap, new Vector2(85, 530), Color.White * alpha);
      sprite_batch.DrawString(font_16, Strings.Controls_LongTap + "\n" + Strings.Controls_LT_Desc_OF.Replace("\\n", "\n"),
          new Vector2(432, 282), Color.SteelBlue * alpha);
      sprite_batch.DrawString(font_16, Strings.Controls_LongTap + "\n" + Strings.Controls_LT_Desc_OF.Replace("\\n", "\n"),
          new Vector2(430, 280), Color.White * alpha);

      sprite_batch.DrawString(font_16, Strings.Pinch.Replace("\\n", "\n"), new Vector2(432, 212), Color.SteelBlue * alpha);
      sprite_batch.DrawString(font_16, Strings.Pinch.Replace("\\n", "\n"), new Vector2(430, 210), Color.White * alpha);

      //sprite_batch.Draw(single_tap, new Vector2(85, 100), Color.White * alpha);
      //sprite_batch.DrawString(font_32, Strings.Controls_SingleTap, new Vector2(150, 120), Color.White * alpha);
      //sprite_batch.DrawString(font_16, Strings.Controls_ST_Desc_OF.Replace("\\n", "\n"), new Vector2(80, 180), Color.White * alpha);
      //sprite_batch.DrawString(font_16, Strings.Controls_ST_Desc_OE.Replace("\\n", "\n"), new Vector2(80, 260), Color.White * alpha);

      //sprite_batch.Draw(double_tap, new Vector2(85, 360), Color.White * alpha);
      //sprite_batch.DrawString(font_32, Strings.Controls_DoubleTap, new Vector2(150, 370), Color.White * alpha);
      //sprite_batch.DrawString(font_16, Strings.Controls_DT_Desc_OE.Replace("\\n", "\n"), new Vector2(80, 430), Color.White * alpha);

      //sprite_batch.Draw(long_tap, new Vector2(85, 530), Color.White * alpha);
      //sprite_batch.DrawString(font_32, Strings.Controls_LongTap, new Vector2(150, 540), Color.White * alpha);
      //sprite_batch.DrawString(font_16, Strings.Controls_LT_Desc_OF.Replace("\\n", "\n"), new Vector2(80, 600), Color.White * alpha);

      sprite_batch.DrawString(font_32, back.Label, back.Position + Vector2.One * 2.5f, Color.SteelBlue * alpha);
      sprite_batch.DrawString(font_32, back.Label, back.Position, Color.White * alpha);
    }

    #endregion

    private Vector2 PlayerShipPos()
    {

      var p = GraphicsDevice.Viewport.Project(state_provider.CurrentState.player.geometry.Position, camera.projection, camera.view, Matrix.Identity);

      return new Vector2(p.X, p.Y);
    }

    private float CenterValue(float screen_dim, float item_width)
    {
      return screen_dim / 2.0f - item_width / 2.0f;
    }
  }
}
